package com.dongdongshop.service.impl;

import com.dongdongshop.api.SellerServiceApi;
import com.dongdongshop.mapper.TbGoodsDescMapper;
import com.dongdongshop.mapper.TbGoodsMapper;
import com.dongdongshop.mapper.TbItemMapper;
import com.dongdongshop.model.TbGoods;
import com.dongdongshop.model.TbGoodsDesc;
import com.dongdongshop.model.TbItem;
import com.dongdongshop.model.TbItemExample;
import com.dongdongshop.service.GoodsService;
import com.dongdongshop.service.ItemCatService;
import com.dongdongshop.util.LoginUser;
import com.dongdongshop.vo.GoodsVO;
import com.dongdongshop.vo.ItemCatVO;
import com.dongdongshop.vo.ItemVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class GoodsServiceImpl implements GoodsService {

    @Resource
    private TbGoodsDescMapper goodsDescMapper;
    @Resource
    private TbGoodsMapper goodsMapper;
    @Resource
    private TbItemMapper itemMapper;
    @Autowired
    private ItemCatService itemCatService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int addGoods(GoodsVO goodsVO, LoginUser loginUser) {
        String userId = loginUser.getUserId();
        goodsVO.setSellerId(userId);
        ItemVO itemVO = goodsVO.getItemVO();
        itemVO.setSeller(loginUser.getUserName());
        itemVO.setSellerId(loginUser.getUserId());
        itemVO.setCreateTime(new Date());
        itemVO.setUpdateTime(new Date());
        itemVO.setStatus("1");
        try {
            //先增加goods表
            TbGoods goods = new TbGoods();
            BeanUtils.copyProperties(goodsVO, goods);
            goods.setAuditStatus("0");
            goodsMapper.insertSelective(goods);
            //返回主键后在增加goodsdesc表
            TbGoodsDesc goodsDesc = new TbGoodsDesc();
            BeanUtils.copyProperties(goodsVO, goodsDesc);
            goodsDesc.setGoodsId(goods.getId());
            goodsDescMapper.insertSelective(goodsDesc);
            TbItem item = new TbItem();
            BeanUtils.copyProperties(itemVO, item);
            item.setGoodsId(goods.getId());
            itemMapper.insertSelective(item);
            return 1;
        }catch (Exception e){
            e.printStackTrace();
            throw e;
        }
    }

    @Override
    public List<GoodsVO> getAllGoods() {
        List<TbGoods> tbGoods = goodsMapper.selectByExample(null);
        List<ItemCatVO> itemCatVOList = itemCatService.getAll();
        List<GoodsVO> collect = tbGoods.stream().map(goods -> {
            GoodsVO vo = new GoodsVO();
            BeanUtils.copyProperties(goods, vo);
            vo.setItemCatVOList(itemCatVOList);
            return vo;
        }).collect(Collectors.toList());

        return collect;
    }

    @Override
    public int changeStatus(List<Long> ids) {
        int i= goodsMapper.changeStatus(ids);
        return i;
    }

    @Override
    public GoodsVO getGoodsById(Long goodsId) {
        //先查询goods表数据
        TbGoods goods = goodsMapper.selectByPrimaryKey(goodsId);
        GoodsVO goodsVO = new GoodsVO();
        BeanUtils.copyProperties(goods,goodsVO);
        //在查询desc表数据
        TbGoodsDesc goodsDesc = goodsDescMapper.selectByPrimaryKey(goodsId);
        BeanUtils.copyProperties(goodsDesc,goodsVO);
        //在查询item数据
        TbItemExample example = new TbItemExample();
        TbItemExample.Criteria criteria = example.createCriteria();
        criteria.andGoodsIdEqualTo(goodsId);
        List<TbItem> tbItems = itemMapper.selectByExample(example);
        List<ItemVO> collect = tbItems.stream().map(item -> {
            ItemVO vo = new ItemVO();
            BeanUtils.copyProperties(item, vo);
            return vo;
        }).collect(Collectors.toList());
        goodsVO.setItemVOList(collect);
        return goodsVO;
    }

    @Override
    public ItemVO getItemById(Long itemId) {
        TbItem item = itemMapper.selectByPrimaryKey(itemId);
        ItemVO vo = new ItemVO();
        BeanUtils.copyProperties(item,vo);
        return vo;
    }
}
